﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmQuery_SupervisionCodeSplit
    /// Version:1.0
    /// Date:2024-10-09
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为电监码拆零
    /// </remarks>
    public partial class frmQuery_SupervisionCodeSplit : DevComponents.DotNetBar.OfficeForm
    {
        #region 01.自定义属性
        #endregion
        public frmQuery_SupervisionCodeSplit()
        {
            InitializeComponent();
        }

        #region 02.自定义方法

        /// <summary>
        /// 初始化DataGridView字段显示信息
        /// </summary>
        public void _initDgvTitle()
        {
            string strFieldName = "", strFieldSql = "", strFieldWidth0 = "";
            string strDgvName = this.Name;
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strDgvName))
            {
                AppConfig._initDictField(strDgvName);
                //return;
            }
            strFieldName = "";
            strFieldSql = "SalerName,Amount,Money,Tax,MoneyTaxNone";
            strFieldWidth0 = "0,200,120";
            AppConfig.m_dictFieldName.Add(strDgvName, strFieldName);
            AppConfig.m_dictFieldSql.Add(strDgvName, strFieldSql);
            AppConfig.m_dictField0.Add(strDgvName, strFieldSql);// 与SqlField同
            AppConfig.m_dictFieldWidth0.Add(strDgvName, strFieldWidth0);
            // 01 设置值
            strFieldSql = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_Field", strDgvName), AppConfig.m_dictField0[strDgvName]).Trim();
            AppConfig.m_dictField.Add(strDgvName, strFieldSql);
            strFieldWidth0 = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_FieldWidth", strDgvName), AppConfig.m_dictFieldWidth0[strDgvName]).Trim();
            AppConfig.m_dictFieldWidth.Add(strDgvName, strFieldWidth0);
            // 02 右键双击设置显示方式
            this.dgvMain.Tag = strDgvName;
            this.dgvMain.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(DataDeal.dataGridView_MouseDoubleClick);
            // 03 其他事件
            this.dgvMain.ColumnHeaderMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(CommFun._dgvColumnHeaderSort);// 排序
            this.dgvMain.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(CommFun._dgvRowEnter);// 改变行颜色
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void _initData()
        {
            // 药房信息
            string strSql = "select PharmacyCode + '|' + PharmacyName as Supplier from TH_PharmacyStockOut group by PharmacyCode,PharmacyName";
            DataTable dt = AppInfo._openSql_DataTable(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                FunctionComm._setInfoToCbb_Dt(cbbPharmacy, dt);
            }
            cbbPharmacy.DrawItem += new System.Windows.Forms.DrawItemEventHandler(CommFun._cbbDrawItem);
            cbbStatus.DrawItem += new System.Windows.Forms.DrawItemEventHandler(CommFun._cbbDrawItem);
        }

        /// <summary>
        /// 取查询条件
        /// </summary>
        private Boolean _getQueryWhere(ref string strWhere)
        {
            Boolean bResult = false;
            //string strDate = "";
            //// 02 取配送日期
            //if (cbDate.Checked)
            //{
            //    if (dtpDateBegin.Value.ToString("yyyy-MM-dd").CompareTo(dtpDateEnd.Value.ToString("yyyy-MM-dd")) > 0)
            //    {
            //        CommFun._messageBoxShow(string.Format("起始日期大于终止日期！"), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return bResult;
            //    }
            //    else
            //    {
            //        strDate = string.Format("and InvoiceDate between '{0}' and '{1}' ", dtpDateBegin.Value.ToString("yyyy-MM-dd"), dtpDateEnd.Value.ToString("yyyy-MM-dd"));
            //    }
            //}            
            //strWhere = string.Format("{0}", strDate);
            bResult = true;
            return bResult;
        }

        /// <summary>
        /// 信息查询
        /// </summary>
        private void _query()
        {
            // 00 取条件:有参数时按参数来取数据
            string strWhere = "";
            if (_getQueryWhere(ref strWhere))
            {
                // 02 取数据                
                string strSql = string.Format(@"exec P_DataQuery 'Query_SupervisionCodeSplit','{0},{1},{2},{3}'", (cbPharmacy.Checked? cbbPharmacy.Text.Trim().Split('|')[0]:"")
                    , (cbGoods.Checked ? txtGoods.Text.Trim() : "")
                    , (cbSupervisionCode.Checked ? txtSupervisionCode.Text.Trim() : "")
                    , (cbStatus.Checked ? cbbStatus.Text.Trim() : ""));
                DataTable dt = AppInfo._openSql_DataTable(strSql);
                // 03 显示数据            
                _showData(dt);
            }            
        }

        /// <summary>
        /// 数据显示
        /// </summary>
        /// <param name="dt"></param>
        private void _showData(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                // 0101 调整字段顺序
                CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[dgvMain.Tag.ToString()]);
                // 0102 显示数据
                CommFun._showDataGridView(dgvMain, dt, AppConfig.m_dictFieldWidth[dgvMain.Tag.ToString()], AppConfig.m_dictFieldName[dgvMain.Tag.ToString()]); 
            }
            else {
                // 清除数据
                DataTable dtTemp = (DataTable)dgvMain.DataSource;
                if (dtTemp != null && dtTemp.Rows.Count > 0)
                {
                    dtTemp.Rows.Clear();
                }      
            }
            lblRecord.Text = string.Format("提示:共有数据信息 {0} 项", dgvMain == null ? 0 : dgvMain.Rows.Count);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 01 dataGridViewX1设为不可编辑
            dgvMain.ReadOnly = true;
            dgvMain.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
            // 05 DataGridView
            _initDgvTitle();
            // 03 初始化配送人员
            _initData();
        }
      
        #endregion

        #region 03.系统事件方法

        private void frm_Load(object sender, EventArgs e)
        {
            _init();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            // 数据查询
            _query();           
        }
     
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dgvMain != null && dgvMain.Rows.Count > 0)
            {
                CommFun._ExportExcel("", dgvMain);
            }
        }
        #endregion

        #region 04 GridViewX事件  

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // 单元格双击操作，对于可编辑的列进行编辑状态，否则不能进行编辑,显示详细信息
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            // 显示明细            
            frmInfoDetail frm = new frmInfoDetail();
            frm.m_nRow = e.RowIndex;
            frm.m_dgv = ((DataGridView)sender);
            frm.ShowDialog();            
        }
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmConfirmSupervisionCode frm = new frmConfirmSupervisionCode();
            frm.ShowDialog();
            if (frm.m_bIsRefresh) { 
                btnQuery_Click(null, null); 
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = dgvMain.CurrentRow;
            if (dr != null)
            {
                DialogResult result = MessageBox.Show("是否确定要删除该拆零信息?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    string strSql = "";
                    int nId = FunctionComm._strToInt(dr.Cells["Id"].Value.ToString());
                    strSql = string.Format("exec P_DealSupervisionCodeSplit 'Delete','{0}'", nId);
                    DataTable dt = AppInfo._openSql_DataTable(strSql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        string strResult = dt.Rows[0]["Result"].ToString();
                        // 未核对成功
                        if (strResult == "0")
                        {
                            MessageBox.Show(string.Format("该拆零信息删除失败！{0}", dt.Rows[0]["Message"].ToString()));
                        }
                        else
                        {
                            MessageBox.Show("该拆零信息删除完成！");
                            dgvMain.Rows.Remove(dr);
                        }
                    }                    
                }
            }
        }
    }
}